//
//  YJVisitorView.swift
//  微博
//
//  Created by YJW on 16/8/10.
//  Copyright © 2016年 yijiang. All rights reserved.
//

import UIKit

class YJVisitorView: UIView {
    
    //登录按钮
    lazy var loginBtn = UIButton(
        title: "登录",
        normalColor: UIColor.darkGray(),
        highlightedColor: UIColor.black(),
        fontSize: 16,
        backgroundImageName: "button_background")

    //注册按钮
    lazy var signUpBtn = UIButton(
        title: "注册",
        normalColor: UIColor.orange(),
        highlightedColor: UIColor.black(),
        fontSize: 16,
        backgroundImageName: "button_background")

    ///设置访客视图的信息字典：[message / imageName]
    ///如果是首页，imageName = “”
    var visitorInfo:[String:String]?{
        
        didSet{
            
            guard let message = visitorInfo?["message"],
                imageName = visitorInfo?["imageViewName"] else {
                
                return
            }
            
            tipLabel.text = message
            
            if imageName == "" {
                //添加旋转动画
                startAnimation()
                return
            }
            
            //设置图像，首页不需要设置
            iconView.image = UIImage(named: imageName)
            
            //隐藏其它控制器的小房子、遮盖视图
            homeView.isHidden = true
            maskIconView.isHidden = true
        }
    }
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //访客视图首页旋转动画
    private func startAnimation(){
        
        let anima = CABasicAnimation(keyPath: "transform.rotation")
        //设置旋转角度
        anima.toValue = M_PI * 2
        //重复次数：设为不停的旋转
        anima.repeatCount = MAXFLOAT
        //旋转一圈的时间
        anima.duration = 15
        
        //动画完成不删除，如果 iconView 被释放，动画会一起被销毁，在设置连续播放的动画时非常有用
        anima.isRemovedOnCompletion = false
        
        //添加到图层
        iconView.layer.add(anima, forKey: nil)
    }

    //图像视图
    private lazy var iconView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    //遮盖图像
    private lazy var maskIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //主页视图
    private lazy var homeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //提示标签
    private lazy var tipLabel = UILabel(text: "关注一些人，回这里看看有什么惊喜",
                                        fontSize: 14,
                                        textColor: UIColor.darkGray())
}

extension YJVisitorView{
    
    func setUpUI(){
        //0.能用颜色解决的问题不要用图像,效率会降低
        backgroundColor = UIColor.init(colorLiteralRed: 237.0/255.0, green: 237.0/255, blue: 237.0/255, alpha: 1)
        
        //1.添加视图
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeView)
        addSubview(tipLabel)
        addSubview(signUpBtn)
        addSubview(loginBtn)
        
        //2.取消 autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //3.设置自动布局
        //3.1.设置 iconView
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1,
                                         constant: -60))
        
        //3.2.设置 homeView
        addConstraint(NSLayoutConstraint(item: homeView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        
        //3.3.设置 tipLabel
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: self.bounds.size.width-60))
        tipLabel.numberOfLines = 0
        tipLabel.textAlignment = .center
        
        //3.4.设置 signUpBtn
        addConstraint(NSLayoutConstraint(item: signUpBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: 40))
        addConstraint(NSLayoutConstraint(item: signUpBtn,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: -40))
        //3.5.设置 loginBtn
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: 40))
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 40))
        
        //3.6.通过 VFL 设置遮盖图像的自动布局
        // views：定义 VFL 中的控件名称和实际名称映射关系
        // metrics：定义 VFL 中 （） 指定的常数映射关系
        let viewDic = ["maskIconView":maskIconView,"tipLabel":tipLabel]
        let metrics = ["space":-10]
        
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-0-[maskIconView]-0-|",
            options: [],
            metrics: nil,
            views: viewDic))
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-0-[maskIconView]-(space)-[tipLabel]",
            options: [],
            metrics: metrics,
            views: viewDic))
    }
}
